﻿using System.Collections.Generic;
using System.Configuration;
using System.Runtime.Serialization;

namespace K3R.Web.Interface {
	[DataContract]
	public class Profile {
		#region Properties
		[DataMember(Name = "code")] public string Code { get; set; }
		[DataMember(Name = "name")] public string Name { get; set; }
		[DataMember(Name = "sqlLogging")] public bool SqlLogging { get; set; }
		[DataMember(Name = "connectionString")] public string ConnectionString { get; set; }
		#endregion

		#region Constructors
		public Profile() { }
		#endregion

		#region Conversion
		internal static Profile Convert(ConfigProfileElement element) {
			var profile = new Profile {
				Code = element.Code,
				Name = element.Name,
				SqlLogging = element.SqlLogging
			};

			if (ConfigurationManager.ConnectionStrings[element.ConnectionStringName] != null)
				profile.ConnectionString = ConfigurationManager.ConnectionStrings[element.ConnectionStringName].ConnectionString;

			return profile;
		}

		internal static ProfileCollection Convert(ConfigProfileCollection elements) {
			var profiles = new ProfileCollection();

			foreach (ConfigProfileElement element in elements)
				profiles.Add(Convert(element));

			return profiles;
		}
		#endregion
	}
}
